package com.ft.oa.service.technologyService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ft.oa.domain.industrys.IndustryContributor;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.domain.technology.TechnologyContributor;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.mapper.technologyMapper.ItechnologyContributorMapper;
import com.ft.oa.service.technologyService.IContributorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ContributorServiceImpl implements IContributorService {
    @Resource
    private ItechnologyContributorMapper itechnologyContributorMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> contributorList(int industryid) {
        QueryWrapper<TechnologyContributor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("technology_id",industryid);
        queryWrapper.eq("dellpeople","0");
        List peoplelist = new ArrayList<>();
        List<TechnologyContributor> technologyContributors = itechnologyContributorMapper.selectList(queryWrapper);
        technologyContributors.forEach(contributorList->{
            HashMap<String,Object> peopleMap = new HashMap<>();
            SysUser sysUser = sysUserMapper.selectById(contributorList.getUserId());
            //参与人用户名
            peopleMap.put("username",sysUser.getUsername());
            //参与人手机号
            peopleMap.put("phone",sysUser.getPhone());
            //参与人id
            peopleMap.put("id",sysUser.getId());
            //技术领域id
            peopleMap.put("technologyId",contributorList.getId());
            peoplelist.add(peopleMap);
        });
        return peoplelist;
    }

    @Override
    public int addpeople(TechnologyContributor itechnologyContributor) {
        int succes = 0;
        List<String> peopleList = itechnologyContributor.getPeopleList();
        for (String list:peopleList) {
            TechnologyContributor contributor = new TechnologyContributor();
            String technologyId = itechnologyContributor.getTechnologyId();
            QueryWrapper<TechnologyContributor> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",list);
            wrapper.eq("technology_id",technologyId);
            wrapper.eq("dellpeople","0");
            List<TechnologyContributor> technologyContributors = itechnologyContributorMapper.selectList(wrapper);
            if(technologyContributors.size()!=0){
                contributor.setDellpeople("0");
                itechnologyContributorMapper.update(contributor,wrapper);
            }else{
                contributor.setUserId(list);
                contributor.setTechnologyId(technologyId);
                int inserts = itechnologyContributorMapper.insert(contributor);
                succes+=inserts;
            }
        }
        return succes;
    }

    @Override
    public int dellContributor(int peopleid) {
        TechnologyContributor technologyContributor = new TechnologyContributor();
        technologyContributor.setDellpeople("1");
        QueryWrapper<TechnologyContributor> wrapper = new QueryWrapper<>();
        wrapper.eq("id",peopleid);
        return itechnologyContributorMapper.update(technologyContributor, wrapper);
    }
}
